package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.User;
import com.dzj.fishingcommunity.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@Tag(name = "用户管理", description = "用户管理相关的接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "获取所有用户")
    @GetMapping
    public ResponseResult<List<User>> getAllUsers(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<User> page = new Page<>(current, size);
        Page<User> userPage = userService.page(page);
        return ResponseResult.success(userPage.getRecords());
    }

    @Operation(summary = "根据ID获取用户")
    @GetMapping("/{id}")
    public ResponseResult<User> getUserById(@PathVariable Long id) {
        User user = userService.getById(id);
        return ResponseResult.success(user);
    }

    @Operation(summary = "新增用户")
    @PostMapping
    public ResponseResult<User> addUser(@RequestBody User user) {
        userService.save(user);
        return ResponseResult.success(user);
    }

    @Operation(summary = "更新用户")
    @PutMapping("/{id}")
    public ResponseResult<User> updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
        return ResponseResult.success(user);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return ResponseResult.success(null);
    }
}